package com.kfm.controller;

import com.kfm.model.Account;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@RestController
public class AccountController {

    @GetMapping("/login")
    public ModelAndView toLogin(){
        return new ModelAndView("login");
    }

    @PostMapping("/login")
    public ModelAndView login(ModelAndView mv, Account account, HttpSession session){
        if ("admin".equals(account.getUsername()) && "a123123".equals(account.getPassword())){
            session.setAttribute("user", account);
            mv.setViewName("redirect:index");
        } else {
            mv.addObject("msg", "用户名或密码错误");
            mv.setViewName("login");
        }
        return mv;
    }

    @GetMapping("/index")
    public ModelAndView index(){
        return new ModelAndView("index");
    }
}
